# Copyright (c) 2008-2009 Mikhail Chernov 
#
# This file is part of dcExplorer.
#
# dcExplorer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# dcExplorer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dcExplorer.  If not, see <http://www.gnu.org/licenses/>.

# Scripting class with extensions
package dcExplorer::core::Empire;
use strict;
use utf8;
use core::Planets;
use core::Harrison;
use core::Queue;
use core::Fleets;
use core::AlienFleets;
use core::Players;
use core::DesignHouse;
use server::CommandQueue;

@dcExplorer::core::Empire::props =
	qw(player_id turn race_nature industry_nature unused_resource main_left second_left money_left main_per_turn second_per_turn money_per_turn main_per_turn_spent second_per_turn_spent money_per_turn_spent tehnology race_id t_optimal t_delta bonus_iq bonus_multiply bonus_attack bonus_dr bonus_hp bonus_speed bonus_build_peace bonus_build_war bonus_price bonus_mining);

# Construction methods

sub new 
{
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $self = {};
	$self->{$_} = undef foreach @$class::props;
	$self->{'player_id'} = shift;
	$self->{ThisFleet} = undef;
	$self->{Planets} = new dcExplorer::core::Planets($self);
	$self->{Harrisons} = {};
	$self->{Queues} = {};
	$self->{Fleets} = new dcExplorer::core::Fleets($self);
	$self->{SharedFleets} = new dcExplorer::core::Fleets($self);
	$self->{AlienFleets} = new dcExplorer::core::AlienFleets($self);
	$self->{Players} = new dcExplorer::core::Players;
	$self->{DipRelations} = {};
	$self->{DesignHouse} = new dcExplorer::core::DesignHouse;
	$self->{Log} = [];
	$self->{Variables} = {};
	$self->{Tag} = undef;
	$self->{Message} = undef;
	$self->{Ok} = 1;
	$self->{Commands} = new dcExplorer::server::CommandQueue;
	bless ($self, $class);
	return $self;
}

sub setProp
{
	my $self = shift;
	my $prop = shift;
	my $value = shift;
	$self->{$prop} = $value;
}

sub setVariable
{
	my $self = shift;
	my $variableName = shift;
	my $valueToStore = shift;
	$self->{Variables}->{$variableName} = $valueToStore;
}

sub setThisFleet
{
	my $self = shift;
	my $fleet = shift;
	$self->{ThisFleet} = $fleet;
}

sub setDipRelation
{
	my $self = shift;
	my $player_id = shift;
	my $new_relation = shift;
	$self->{DipRelations}->{$player_id} = $new_relation;
}

# Internal engine methods

sub getEmpire
{
	my $self = shift;
	return $self;
}

# Scripting interface

sub fake
{
	my $self = shift;
	my $fake = shift;
	# TODO: Implement.
	die "Empire::fake is not implemented yet.";
}

sub tag
{
	my $self = shift;
	my $value = shift;
	$self->{Tag} = $value if defined $value;
	# TODO: Add action to store on server / in database.
	return $self->{Tag};
}

sub message
{
	my $self = shift;
	my $value = shift;
	$self->{Message} = $value if defined $value;
	# TODO: Add action to store on server / in database.
	return $self->{Message};
}

sub ok
{
	my $self = shift;
	my $value = shift;
	$self->{Ok} = $value if defined $value;
	# TODO: Add action to store on server / in database.
	return $self->{Ok};
}

sub this_fleet
{
	my $self = shift;
	return $self->{ThisFleet};
}

sub harrison
{
	my $self = shift;
	my $x = shift;
	my $y = shift;
	$self->{Harrisons}->{$x.":".$y} = dcExplorer::core::Harrison->new($self, $x, $y) unless defined($self->{Harrisons}->{$x.":".$y});
	return $self->{Harrisons}->{$x.":".$y};
}

sub dipRelation
{
	my $self = shift;
	my $player_id = shift;
	return $self->{DipRelations}->{$player_id};
}

sub fleets
{
	my $self = shift;
	return $self->{Fleets};
}

sub planets
{
	my $self = shift;
	return $self->{Planets};
}

sub queue
{
	my $self = shift;
	my $x = shift;
	my $y = shift;
	$self->{Queues}->{$x.":".$y} = dcExplorer::core::Queue->new($self, $x, $y) unless defined($self->{Queues}->{$x.":".$y});
	return $self->{Queues}->{$x.":".$y};
}

sub designHouse
{
	my $self = shift;
	return $self->{DesignHouse};
}

sub log
{
	my $self = shift;
	my $status = shift;
	my $description = shift;
	push @{$self->{Log}}, [$status, $description];
}

sub writeVariable
{
	my $self = shift;
	my $variableName = shift;
	my $valueToStore = shift;
	$self->setVariable($variableName, $valueToStore);
	# TODO: Add action to store on server / in database.
}

sub readVariable
{
	my $self = shift;
	my $variableName = shift;
	return $self->{Variables}->{$variableName};
}

sub getVariablesNames
{
	my $self = shift;
	return keys %{$self->{Variables}};
}

sub clearLog
{
	my $self = shift;
	$self->{Log} = [];
}

sub getProp
{
	my $self = shift;
	my $prop = shift;
	return $self->{$prop};
}

# Scripting extensions

sub shared_fleets
{
	my $self = shift;
	return $self->{SharedFleets};
}

sub alien_fleets
{
	my $self = shift;
	return $self->{AlienFleets};
}

sub players
{
	my $self = shift;
	return $self->{Players};
}

sub commandQueue
{
	my $self = shift;
	return $self->{Commands};
}

1;
